﻿using MyTodo.Common;
using MyTodo.Common.Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyTodo.ViewModels
{
    public class MainViewModel : BindableBase
    {
        private IRegionManager RegionManager;
        private IRegionNavigationJournal regionNavigationJournal;
        public MainViewModel(IRegionManager regionManager)
        {
            menuBars = new ObservableCollection<MenuBar>();
            CreateMenus();
            this.RegionManager = regionManager;

            NavgationCommand = new DelegateCommand<MenuBar>(Navgation);
            GoBackCommand = new DelegateCommand(GoBack);
            GoForwordCommand = new DelegateCommand(GoForword);
            
        }

        private void GoForword()
        {
            if (regionNavigationJournal != null && regionNavigationJournal.CanGoForward)
            {
                regionNavigationJournal.GoForward();
            }
        }

        private void GoBack()
        {
            if (regionNavigationJournal != null && regionNavigationJournal.CanGoBack)
            {
                regionNavigationJournal.GoBack();
            }
        }

        private DelegateCommand mouseMoveCommand;
        /// <summary>
        /// 打开新试图
        /// </summary>
        /// <param name="obj"></param>
        private void Navgation(MenuBar obj)
        {
            if (string.IsNullOrEmpty(obj.ViewName))
                return;
            this.RegionManager.Regions[PrismRegionManager.MainViewRegionName].RequestNavigate(obj.ViewName, (callback) =>
            {
                //导航成功则记录导航日志
                if ((bool)callback.Result)
                {
                    regionNavigationJournal = callback.Context.NavigationService.Journal;
                }
            });
        }



        /// <summary>
        /// 列表
        /// </summary>
        private ObservableCollection<MenuBar> menuBars;
        public ObservableCollection<MenuBar> MenuBars
        {
            get
            {
                return menuBars;
            }
            set
            {
                menuBars = value;
                this.RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 列表切换命令
        /// </summary>
        private DelegateCommand<MenuBar> navgationCommand;
        /// <summary>
        /// 导航命令
        /// </summary>
        public DelegateCommand<MenuBar> NavgationCommand { get => navgationCommand; set => navgationCommand = value; }

        /// <summary>
        /// 后退
        /// </summary>
        private DelegateCommand goBackCommand;
        public DelegateCommand GoBackCommand { get => goBackCommand; set => goBackCommand = value; }

        /// <summary>
        /// 向前翻页
        /// </summary>
        private DelegateCommand goForwordCommand;
        public DelegateCommand GoForwordCommand { get => goForwordCommand; set => goForwordCommand = value; }
     

        void CreateMenus()
        {
            menuBars.Add(new MenuBar { Icao = "Home", Title = "首页", ViewName = "IndexView" });
            menuBars.Add(new MenuBar { Icao = "Notebook", Title = "待办事项", ViewName = "NotebookView" });
            menuBars.Add(new MenuBar { Icao = "NotebookPlus", Title = "备忘录", ViewName = "NotebookPlusView" });
            menuBars.Add(new MenuBar { Icao = "Cog", Title = "设置", ViewName = "SettingsView" });
        }
    }
}
